﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ITSTOREBLL;
using ITStore.Models;
using System.Web.Security;

namespace ITStore.Controllers
{
    //[Authorize(Roles = "1")]
    public class AdminController : Controller
    {
        private ITStoreEntities db = new ITStoreEntities();

        //
        // GET: /Admin/

        public ViewResult Index()
        {
            var taikhoans = db.TaiKhoans.Include("GioiTinh").Include("LoaiTaiKhoan").Include("TinhThanh");
            return View(taikhoans.ToList());
        }

        //
        // GET: /Admin/Details/5

        public ViewResult TaiKhoanDetails(int id)
        {
            TaiKhoan taikhoan = db.TaiKhoans.Single(t => t.tk_ID == id);
            return View(taikhoan);
        }

        //
        // GET: /Admin/Create

        public ActionResult TaiKhoanCreate()
        {
            ViewBag.tk_GioiTinh = new SelectList(db.GioiTinhs, "gt_ID", "gt_Loai");
            ViewBag.tk_Loai = new SelectList(db.LoaiTaiKhoans, "ltk_ID", "ltk_Ten");
            ViewBag.tk_TinhThanh = new SelectList(db.TinhThanhs, "tt_ID", "tt_Ten");
            return View();
        }

        //
        // POST: /Admin/Create

        [HttpPost]
        public ActionResult TaiKhoanCreate(TaiKhoan taikhoan)
        {
            if (ModelState.IsValid)
            {
                MembershipCreateStatus createStatus;
                Membership.CreateUser(taikhoan.tk_TenDangNhap, taikhoan.tk_MatKhau, taikhoan.tk_Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(taikhoan.tk_TenDangNhap, false /* createPersistentCookie */);
                    Roles.AddUserToRole(taikhoan.tk_TenDangNhap, Request.Form["tk_Loai"]);
                    db.TaiKhoans.AddObject(taikhoan);
                    db.SaveChanges();
                    return RedirectToAction("Index", "admin");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            ViewBag.tk_GioiTinh = new SelectList(db.GioiTinhs, "gt_ID", "gt_Loai", taikhoan.tk_GioiTinh);
            ViewBag.tk_Loai = new SelectList(db.LoaiTaiKhoans, "ltk_ID", "ltk_Ten", taikhoan.tk_Loai);
            ViewBag.tk_TinhThanh = new SelectList(db.TinhThanhs, "tt_ID", "tt_Ten", taikhoan.tk_TinhThanh);
            return View(taikhoan);
        }

        //
        // GET: /Admin/Edit/5

        public ActionResult TaiKhoanEdit(int id)
        {
            TaiKhoan taikhoan = db.TaiKhoans.Single(t => t.tk_ID == id);
            ViewBag.tk_GioiTinh = new SelectList(db.GioiTinhs, "gt_ID", "gt_Loai", taikhoan.tk_GioiTinh);
            ViewBag.tk_Loai = new SelectList(db.LoaiTaiKhoans, "ltk_ID", "ltk_Ten", taikhoan.tk_Loai);
            ViewBag.tk_TinhThanh = new SelectList(db.TinhThanhs, "tt_ID", "tt_Ten", taikhoan.tk_TinhThanh);
            return View(taikhoan);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        public ActionResult TaiKhoanEdit(TaiKhoan taikhoan)
        {
            if (ModelState.IsValid)
            {
                db.TaiKhoans.Attach(taikhoan);
                db.ObjectStateManager.ChangeObjectState(taikhoan, EntityState.Modified);
                Roles.RemoveUserFromRoles(taikhoan.tk_TenDangNhap, Roles.GetRolesForUser());
                Roles.AddUserToRole(taikhoan.tk_TenDangNhap, Request.Form["tk_Loai"]);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.tk_GioiTinh = new SelectList(db.GioiTinhs, "gt_ID", "gt_Loai", taikhoan.tk_GioiTinh);
            ViewBag.tk_Loai = new SelectList(db.LoaiTaiKhoans, "ltk_ID", "ltk_Ten", taikhoan.tk_Loai);
            ViewBag.tk_TinhThanh = new SelectList(db.TinhThanhs, "tt_ID", "tt_Ten", taikhoan.tk_TinhThanh);
            return View(taikhoan);
        }

        //
        // GET: /QLMenu/Details/5
        public ViewResult QuanLyMenu()
        {
            return View(db.Menus.ToList());
        }

        //
        // GET: /QLMenu/Details/5

        public ViewResult QuanLyMenuDetails(int id)
        {
            Menu menu = db.Menus.Single(m => m.mn_ID == id);
            return View(menu);
        }

        //
        // GET: /QLMenu/Create

        public ActionResult QuanLyMenuCreate()
        {
            return View();
        }

        //
        // POST: /QLMenu/Create

        [HttpPost]
        public ActionResult QuanLyMenuCreate(Menu menu)
        {
            if (ModelState.IsValid)
            {
                db.Menus.AddObject(menu);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(menu);
        }

        //
        // GET: /QLMenu/Edit/5

        public ActionResult QuanLyMenuEdit(int id)
        {
            Menu menu = db.Menus.Single(m => m.mn_ID == id);
            return View(menu);
        }

        //
        // POST: /QLMenu/Edit/5

        [HttpPost]
        public ActionResult QuanLyMenuEdit(Menu menu)
        {
            if (ModelState.IsValid)
            {
                db.Menus.Attach(menu);
                db.ObjectStateManager.ChangeObjectState(menu, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("QuanLyMenu");
            }
            return View(menu);
        }

        public ActionResult QuanLyMenuDelete(int id)
        {
            Menu menu = db.Menus.Single(l => l.mn_ID == id);
            return View(menu);
        }

        //
        // POST: /QLLoaiSanPham/Delete/5

        [HttpPost, ActionName("QuanLyMenuDelete")]
        public ActionResult QuanLyMenuDeleteConfirmed(int id)
        {
            Menu menu = db.Menus.Single(l => l.mn_ID == id);
            db.Menus.DeleteObject(menu);
            db.SaveChanges();
            return RedirectToAction("QuanLyMenu");
        }

        public ViewResult QuanLyDonHang()
        {
            var donhangs = db.DonHangs.Include("TrangThaiDonHang");
            return View(donhangs.ToList());
        }

        public ViewResult QuanLyDonHangEdit(Guid id)
        {
            DonHang donhang = db.DonHangs.Single(l => l.dh_ID == id);
            ViewBag.dh_TrangThai = new SelectList(db.TrangThaiDonHangs, "tt_ID", "tt_Ten",donhang.dh_TrangThai);
            return View(donhang);
        }

        //
        // POST: /QuanLyDonHang/Edit/5

        [HttpPost]
        public ActionResult QuanLyDonHangEdit(DonHang donhang)
        {
            if (ModelState.IsValid)
            {
                db.DonHangs.Attach(donhang);
                db.ObjectStateManager.ChangeObjectState(donhang, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("QuanLyDonHang");
            }
            ViewBag.dh_TrangThai = new SelectList(db.TrangThaiDonHangs, "tt_ID", "tt_Ten", donhang.dh_TrangThai);
            return View(donhang);
        }

        public ActionResult QuanLyDonHangDelete(Guid id)
        {
            DonHang donhang = db.DonHangs.Single(l => l.dh_ID == id);
            return View(donhang);
        }

        public ViewResult QuanLyLSP()
        {
            return View(db.LoaiSanPhams.ToList());
        }

        //
        // GET: /QLLoaiSanPham/Details/5

        public ViewResult QuanLyLSPDetails(int id)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Single(l => l.lsp_ID == id);
            return View(loaisanpham);
        }

        //
        // GET: /QLLoaiSanPham/Create

        public ActionResult QuanLyLSPCreate()
        {
            return View();
        }

        //
        // POST: /QLLoaiSanPham/Create

        [HttpPost]
        public ActionResult QuanLyLSPCreate(LoaiSanPham loaisanpham)
        {
            if (ModelState.IsValid)
            {
                db.LoaiSanPhams.AddObject(loaisanpham);
                db.SaveChanges();
                return RedirectToAction("QuanLyLSP");
            }

            return View(loaisanpham);
        }

        //
        // GET: /QLLoaiSanPham/Edit/5

        public ActionResult QuanLyLSPEdit(int id)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Single(l => l.lsp_ID == id);
            return View(loaisanpham);
        }

        //
        // POST: /QLLoaiSanPham/Edit/5

        [HttpPost]
        public ActionResult QuanLyLSPEdit(LoaiSanPham loaisanpham)
        {
            if (ModelState.IsValid)
            {
                db.LoaiSanPhams.Attach(loaisanpham);
                db.ObjectStateManager.ChangeObjectState(loaisanpham, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("QuanLyLSP");
            }
            return View(loaisanpham);
        }

        //
        // GET: /QLLoaiSanPham/Delete/5

        public ActionResult QuanLyLSPDelete(int id)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Single(l => l.lsp_ID == id);
            return View(loaisanpham);
        }

        //
        // POST: /QLLoaiSanPham/Delete/5

        [HttpPost, ActionName("QuanLyLSPDelete")]
        public ActionResult QuanLyLSPDeleteConfirmed(int id)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Single(l => l.lsp_ID == id);
            db.LoaiSanPhams.DeleteObject(loaisanpham);
            db.SaveChanges();
            return RedirectToAction("QuanLyLSP");
        }

        public ViewResult QuanLySanPham()
        {
            var sanphams = db.SanPhams.Include("LoaiSanPham").Include("NhaSanXuat");
            return View(sanphams.ToList());
        }

        //
        // GET: /QuanLySanPham/Details/5

        public ViewResult QuanLySanPhamDetails(int id)
        {
            SanPham sanpham = db.SanPhams.Single(s => s.sp_ID == id);
            return View(sanpham);
        }

        //
        // GET: /QuanLySanPham/Create

        public ActionResult QuanLySanPhamCreate()
        {
            ViewBag.sp_Loai = new SelectList(db.LoaiSanPhams, "lsp_ID", "lsp_Ten");
            ViewBag.sp_NhaSanXuat = new SelectList(db.NhaSanXuats, "nsx_ID", "nsx_Ten");
            return View();
        }

        //
        // POST: /QuanLySanPham/Create

        [HttpPost]
        public ActionResult QuanLySanPhamCreate(SanPham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.SanPhams.AddObject(sanpham);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.sp_Loai = new SelectList(db.LoaiSanPhams, "lsp_ID", "lsp_Ten", sanpham.sp_Loai);
            ViewBag.sp_NhaSanXuat = new SelectList(db.NhaSanXuats, "nsx_ID", "nsx_Ten", sanpham.sp_NhaSanXuat);
            return View(sanpham);
        }

        //
        // GET: /QuanLySanPham/Edit/5

        public ActionResult QuanLySanPhamEdit(int id)
        {
            SanPham sanpham = db.SanPhams.Single(s => s.sp_ID == id);
            ViewBag.sp_Loai = new SelectList(db.LoaiSanPhams, "lsp_ID", "lsp_Ten", sanpham.sp_Loai);
            ViewBag.sp_NhaSanXuat = new SelectList(db.NhaSanXuats, "nsx_ID", "nsx_Ten", sanpham.sp_NhaSanXuat);
            return View(sanpham);
        }

        //
        // POST: /QuanLySanPham/Edit/5

        [HttpPost]
        public ActionResult QuanLySanPhamEdit(SanPham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.SanPhams.Attach(sanpham);
                db.ObjectStateManager.ChangeObjectState(sanpham, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.sp_Loai = new SelectList(db.LoaiSanPhams, "lsp_ID", "lsp_Ten", sanpham.sp_Loai);
            ViewBag.sp_NhaSanXuat = new SelectList(db.NhaSanXuats, "nsx_ID", "nsx_Ten", sanpham.sp_NhaSanXuat);
            return View(sanpham);
        }

        //
        // GET: /QuanLySanPham/Delete/5

        public ActionResult QuanLySanPhamDelete(int id)
        {
            SanPham sanpham = db.SanPhams.Single(s => s.sp_ID == id);
            return View(sanpham);
        }

        //
        // POST: /QuanLySanPham/Delete/5

        [HttpPost, ActionName("QuanLySanPhamDelete")]
        public ActionResult QuanLySanPhamDeleteConfirmed(int id)
        {
            SanPham sanpham = db.SanPhams.Single(s => s.sp_ID == id);
            db.SanPhams.DeleteObject(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #region Status Codes
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion
    }
}